#include <normalCanvas.h>
#include <iostream>

#include <uiResourceManager.h>

namespace lgb_gtk
{
  NormalCanvas::NormalCanvas()
  {
    x = 0;
    y = 0;
    width = 200;
    height = 30;

    bgPath = "";
    bg = NULL;
    canvasForChild = goo_canvas_group_new( NULL , NULL);
    canvasForBg = goo_canvas_group_new( canvasForChild , NULL);
  }

  NormalCanvas::~NormalCanvas()
  {

  }

  int NormalCanvas::Init()
  {
    if(hasInit)
      return 1;
    g_object_set(canvasForChild , "parent" , canvas,NULL);
    //goo_canvas_item_set_parent(canvasForChild , canvas);
    if("" != bgPath)
      {
	DoSetBackground(bgPath);
      }

    hasInit = true;
    return 0;
  }

  int NormalCanvas::SetBackground(string newBgPath) 
  {
    if("" == newBgPath)
      return 1;

    bgPath = newBgPath;
    if(hasInit)
      {
	DoSetBackground(newBgPath);
      }
    return 0;
  }

  int NormalCanvas::DoSetBackground(string newBgPath) 
  {
    GdkPixbuf *pixbuf = 
      UiResourceManager::GetInstance()->CreatePixbuf(newBgPath);
    if(NULL == pixbuf)
      return 1;
    
    if(NULL != bg)
      g_object_unref(bg);
    bg = goo_canvas_image_new( canvasForBg , pixbuf , 0 , 0 , NULL);
    g_object_unref(pixbuf);
    return 0;
  }

}
